package org.tale.bootbase.controller;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @program: boot-base
 * @author: tale-mi
 * @description:
 * @create: 2021/4/27 17:35
 **/
@RestController
@RequestMapping("consumer")
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    @NacosInjected
    private NamingService namingService;
    
    @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
    public String echo(@PathVariable String str) throws NacosException {
        Instance instance=namingService.selectOneHealthyInstance("boot-base-nacos","demo-group");
        String url = String.format("http://%s:%d/discovery/echo/"+str, instance.getIp(), instance.getPort());
        String result=restTemplate.getForObject(url, String.class);
        System.out.println(String.format("请求URL:%s,响应结果:%s", url, result));return result;
    }
}
